package cn.wwcyc.controller;

import cn.wwcyc.domain.Menu;
import cn.wwcyc.domain.R;
import cn.wwcyc.domain.Role;
import cn.wwcyc.exception.WWExceptionEnum;
import cn.wwcyc.mapper.MenuMapper;
import cn.wwcyc.mapper.RoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;


@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    MenuMapper mm;

    @GetMapping("/getlist")
    public R<List<Menu>> getList(){
        LambdaQueryWrapper<Menu> qw = new LambdaQueryWrapper();
        qw.isNull(Menu::getPid);
        qw.orderByAsc(Menu::getXh);
        List<Menu> list = mm.selectList(qw);
        for(Menu menu : list){
            addChildren(menu);
        }
        return R.success(list);
    }

    //查出某个菜单的所有子菜单，级联所有
    private void addChildren(Menu menu) {
        LambdaQueryWrapper<Menu> qw1 = new LambdaQueryWrapper();
        qw1.eq(Menu::getPid, menu.getId());
        qw1.orderByAsc(Menu::getXh);
        List<Menu> menuList = mm.selectList(qw1);
        if(menuList == null || menuList.size() == 0){
            return;
        }else{
            menu.setChildren(menuList);
            for(Menu menu1 : menuList){
                addChildren(menu1);
            }
        }
    }

//    //在某个菜单下添加新的子菜单项
//    @PostMapping("/addChildMenu")
//    public R addChildMenu(Integer parentId, Menu childMenu){
//        childMenu.setPid(parentId);
//        mm.insert(childMenu);
//        return R.success();
//    }

    @RequestMapping("/getpage")
    public R<IPage<Menu>> getPage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required=false) String name

            ){
        LambdaQueryWrapper<Menu> qw = new LambdaQueryWrapper();
        if(!StringUtils.isBlank(name))
            qw.like(Menu::getName, name);
        qw.isNull(Menu::getPid);
        IPage<Menu> page = new Page<>(pageNum, pageSize);
        page = mm.selectPage(page, qw);
        for(Menu menu: page.getRecords()){
            addChildren(menu);
        }
        return R.success(page);
    }

    //测试用函数
    @GetMapping("/getpage1")
    public IPage<Role> getPage1(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required=false) String name

    ){
        LambdaQueryWrapper<Menu> qw = new LambdaQueryWrapper();
        if(!StringUtils.isBlank(name))
            qw.like(Menu::getName, name);

        IPage<Menu> page = new Page<>(pageNum, pageSize);
        page = mm.selectPage(page, qw);
        throw new RuntimeException("qwqwqw");

    }

    @PostMapping
    public R<Integer> save(@RequestBody Menu menu){
        Integer i = mm.insert(menu);
        return R.success(i);
    }

    @PostMapping("/edit")
    public R<Integer> updateById(@RequestBody Menu menu){
        System.out.println(menu);
        Integer i = mm.updateById(menu);
        return R.success(i);
    }

    @DeleteMapping
    public R<Integer> delete(@RequestParam Long id){
        System.out.println(id);
        try{
            Integer i = mm.deleteById(id);
            return R.success(i);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return R.error(WWExceptionEnum.SYSTEM_ERROR);
        }
    }
}
